<?php

/**
 * Created by PhpStorm.
 * User: dev
 * Date: 29/9/14
 * Time: 11:27 AM
 */
class Commoncall
{


    public static $mail_para;


    public function __construct()
    {
        parent::__construct();
    }


    public static function postLink($post_id)
    {
        $url = '#';
        if (!empty($post_id)) {

            $last_cat = Terms::category($post_id, true);
            $last_loc = Terms::location($post_id, true);


            if ($last_cat && $last_loc) {

                $loc = Str::slug(Locations::findPK(Terms::location($post_id, true))->name);
                $last_cat = Str::slug(Categories::findPK(Terms::category($post_id, true))->name);
                $title = Str::slug(Post::findPK($post_id)->title);
                $url = $loc . '/' . $title . '/' . $last_cat . '/' . $post_id;
                return $url;
            }
        }
    }

    public static function validate_post_data($post_id)
    {
        $sql1 = "select value from terms where post_id = $post_id and term = 'category' limit 0,1";
        $sql2 = "select value from terms where post_id = $post_id and term = 'location' limit 0,1";

        $vCat = DB::query($sql1);
        $vLoc = DB::query($sql2);

        if ($vCat && $vLoc) {
            return true;
        }


    }

    public static function current_timestamp()
    {
        // set timezone to user timezone
        date_default_timezone_set(Settings::get('en', 'timezone'));
        return new DateTime;
    }

    public static function get_ip() {
    	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
    		$ip = $_SERVER['HTTP_CLIENT_IP'];
    	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
    		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    	} else {
    		$ip = $_SERVER['REMOTE_ADDR'];
    	}
    	return $ip;
    }

    public static function mail($parameter)
    {

        static::$mail_para = $parameter;

        if (Settings::get('en', 'mail_smtp') == 1) {

            //GET SETTINGS
            $host = trim(Settings::get('en', 'mail_smtp_host'));
            $port = trim(Settings::get('en', 'mail_smtp_port'));
            $encryption = 'ssl';
            $username = trim(Settings::get('en', 'mail_smtp_user'));
            $password = trim(Settings::get('en', 'mail_smtp_password'));

            Config::set('messages::config.default', 'smtp');
            Config::set('messages::config.transports.smtp.host', $host);
            Config::set('messages::config.transports.smtp.port', $port);
            Config::set('messages::config.transports.smtp.username', $username);
            Config::set('messages::config.transports.smtp.password', $password);
            Config::set('messages::config.transports.smtp.encryption', $encryption);

            Message::send(function ($message) {
                $para = static::$mail_para;
                $to = trim($para['to']);
                $from = trim(Settings::get('en', 'mail_smtp_user'));
                $senderName = $para['sender_name'];
                $replyTo = $para['reply_to'];
                $body = $para['body'];
                $subject = $para['subject'];

                $message->to($to);
                $message->from($from, $senderName);
                $message->reply($replyTo);

                $message->subject($subject);
                $message->body($body);
                $message->html(true);
            });

        } else {
            //--USE PHP MAIL
            Config::set('messages::config.default', 'mail');
            Message::send(function ($message) {
                $para = static::$mail_para;
                $message->to($para['to']);
                $message->from($para['from'], $para['sender_name']);
                $message->reply($para['reply_to']);

                $message->subject($para['subject']);
                //$message->body('view: emails.hello');
                $message->body($para['body']);
                // You can add View data by simply setting the value
                // to the message.
                //$message->body->name = 'Someone';
                $message->html(true);
            });
        }
        if (Message::was_sent()) {
            return true;
        }
    }


}